package backend.project.gym_management.controller;

import java.util.List;

import backend.project.gym_management.base.response.JsonResult;
import backend.project.gym_management.base.response.StatusCode;
import backend.project.gym_management.pojo.dto.UserHealData;
import backend.project.gym_management.pojo.vo.UserCourseDetailVO;
import backend.project.gym_management.pojo.vo.UserCourseListVO;
import backend.project.gym_management.pojo.vo.UserDetailsVO;
import backend.project.gym_management.pojo.vo.UserHealthDataVO;
import backend.project.gym_management.service.UserService;
import backend.project.gym_management.pojo.entity.PayOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * 用户功能模块的控制器类，处理与用户相关的请求
 */
@Api(tags = "05.用户功能模块")
@Slf4j
@RestController
@RequestMapping("/v1/user/")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 查看课程列表
     *
     * @param uid 用户ID
     * @return 课程列表
     */
    @ApiOperation("查看课程列表")
    @ApiOperationSupport(order = 10)
    @GetMapping("course/list")
    public JsonResult listCourses(@RequestParam Long uid) {
        log.debug("开始处理查看课程列表请求，用户ID：{}", uid);
        List<UserCourseListVO> courses = userService.listCourses(uid);
        log.debug("查看课程列表成功，结果：{}", courses);
        return JsonResult.ok(courses);
    }

    /**
     * 选课操作
     *
     * @param param 选课参数，包括用户ID和课程ID
     * @return 选课结果
     */
//    @ApiOperation("选课操作")
//    @ApiOperationSupport(order = 20)
//    @PostMapping("course/select")
//    public JsonResult selectCourse(@RequestBody UserSelectCourseParam param) {
//        log.debug("开始处理选课操作请求，参数：{}", param);
//
//        try {
//            UserEnrollmentInfoVO result = userService.selectCourse(param);
//            return JsonResult.ok(result);
//        } catch (Exception e) {
//            e.printStackTrace();
//            return new JsonResult(StatusCode.INVALID_PARAMETER, "选课失败：" + e.getMessage());
//        }
//    }

    /**
     * 查看课程详情
     *
     * @param cid 课程ID
     * @return 课程详情
     */
    @ApiOperation("查看课程详情")
    @ApiOperationSupport(order = 30)
    @GetMapping("course/detail/{cid}")
    public JsonResult getCourseDetail(@PathVariable Long cid) {
        log.debug("开始处理查看课程详情请求，参数：{}", cid);
        UserCourseDetailVO detail = userService.getCourseDetail(cid);
        if (detail != null) {
            log.debug("查看课程详情成功，结果：{}", detail);
            return JsonResult.ok(detail);
        } else {
            return new JsonResult(StatusCode.INVALID_PARAMETER, "课程ID不存在");
        }
    }

    /**
     * 查看用户健康数据
     *
     * @param uid 用户ID
     * @return 用户健康数据
     */
    @ApiOperation("查看用户健康数据")
    @ApiOperationSupport(order = 40)
    @GetMapping("health/data/{uid}")
    public JsonResult getHealthData(@PathVariable Long uid) {
        log.debug("开始处理查询用户健康数据请求，用户ID：{}", uid);
        try {
            UserHealthDataVO result = userService.getHealthDataByUid(uid);
            if (result == null) {
                return new JsonResult(StatusCode.OPERATION_FAILED, "数据查找失败");
            }
            return JsonResult.ok(result);
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(StatusCode.UNKNOW_ERROR, "未知异常");
        }
    }

    /**
     * 获取用户详细信息
     * @param uid
     * @return JsonResult
     */
    @ApiOperation("获取用户详细信息")
    @ApiOperationSupport(order = 50)
    @GetMapping("details/{uid}")
    public JsonResult getUserDetails(@PathVariable Long uid) {
        log.debug("开始处理获取用户详细信息请求，用户ID：{}", uid);
        try {
            UserDetailsVO userDetails = userService.getUserDetails(uid);
            return JsonResult.ok(userDetails);
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(StatusCode.UNKNOW_ERROR, "获取用户信息失败");
        }
    }

    /**
     * 插入用户健康数据
     * @param healthData
     * @return JsonResult
     */
    @ApiOperation("插入用户健康数据")
    @ApiOperationSupport(order = 60)
    @PostMapping("health/insert")
    public JsonResult insertHealthData(@RequestBody UserHealData healthData) {
        log.debug("开始处理插入用户健康数据请求，参数：{}", healthData);
        try {
            userService.updateHealthData(healthData);
            return JsonResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(StatusCode.UNKNOW_ERROR, "插入健康数据失败");
        }
    }

    @ApiOperation("插入用户订单信息")
    @ApiOperationSupport(order = 70)
    @PostMapping("order/insert")
    public JsonResult insertOrder(@RequestBody PayOrder order) {
        log.debug("开始处理插入用户订单信息请求，参数：{}", order);
        Long oid = userService.insertOrder(order);
        log.debug("oid参数：{}", oid);
        return JsonResult.ok(oid);
    }

    @ApiOperation("查询用户订单信息")
    @ApiOperationSupport(order = 80)
    @GetMapping("order/select/{uid}")
    public JsonResult selectOrder(@PathVariable Long uid) {
        log.debug("开始处理查询用户订单信息请求，参数：{}", uid);
        List<PayOrder> order = userService.selectOrder(uid);
        log.debug("order参数：{}", order);
        return JsonResult.ok(order);
    }
}
